/**
  ******************************************************************************
  * @file    py32f031_it.c
  * @author  MCU Application Team
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by Puya under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "py32f031_it.h"

/* Private includes ----------------------------------------------------------*/

#include "myTasks.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* External variables --------------------------------------------------------*/

/******************************************************************************/
/*          Cortex-M0+ Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
    LOG_STR("\n\nHARD FAULT\n\n");
  while (1)
  {
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  HAL_IncTick();
    rtx_Sys_tick_tack();
}

/******************************************************************************/
/* PY32F031 Peripheral Interrupt Handlers                                     */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file.                                          */
/******************************************************************************/

volatile uint8_t flag_click_time_protect = 1; // 默认为 1，避免上电后松手产生的按键抖动下降沿触发按键事件

void alarm_cb_debounce(void *param);

struct rtx_Alarm_stu alarm_debounce = {
    .flag = 0,

    .tick_count_down = 0,
    .callback_alarm = alarm_cb_debounce,

    .next = NULL,
};

// 消抖闹钟
void alarm_cb_debounce(void *param){

    if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)){ // 高电平，按键松开消抖完毕
        flag_click_time_protect = 0; // 关闭保护
    }else { // 低电平，按键按下消抖完毕
        // 定期扫描，避免丢失中断导致的保护锁死以致按键不灵敏
        rtx_Alarm_set_count(&alarm_debounce, 50);
        rtx_Alarm_add(&alarm_debounce);
    }
}

// 改成汇编或者搬到 ram 跑应该会更快进入
/**
  * @brief This function handles EXTI2_3 Interrupt .
  */
void EXTI2_3_IRQHandler(void)
{
    UsType_t us_here = rtx_Sys_get_us();
    // uint32_t us_here = SysTick->VAL;
    // us_here *= 1000;
    // us_here /= SysTick->LOAD;

    // us_here = 1000 - us_here;

    // 清除中断标志位
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_3);

    if(flag_click_time_protect == 0){ // 抖动中的首次下降沿
        // tester.flag_last_pin_state = 0;

        // 记录按键按下的时间
        tester.click_us = us_here;
        tester.click_ms = rtx_Sys_get_tick();
        // 发布按键按下事件
        rtx_Topic_publish(&topic_tester);

        flag_click_time_protect = 1; // 进入首次下降沿时间保护
    }

    // 重置消抖时钟
    rtx_Alarm_set_count(&alarm_debounce, 20);
    rtx_Alarm_add(&alarm_debounce);

}


/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
